package recomendation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;

import io.*;
import track.*;
import auth.*;

public class Recomendation {

	public static void doLike(playlistEntry song, boolean isLike) {
		int action = -1; //Dislike
		if(isLike)
			action = 1; //Like
		int genreId = song.getG().getId();
		int artistId = song.getArtist().getId();
		int userId = auth.Identity.getIdentity().getId();

		ResultSet r = DbFuncs.getContent("SELECT COUNT(*) FROM genre2user WHERE genre_id='" + genreId +  
											"' AND user_id='" + userId + "'");
		int numOfResults = -1;
		
		try {
			numOfResults = r.getInt(1);
		}
		catch (SQLException e) {
			System.out.println("Exception: " + e.getMessage() );
		}
		if(numOfResults == 0) {
			SqlDb.executeQuery("INSERT INTO genre2user(genre_id, user_id, rating) VALUES('" + genreId + 
								"',' " + userId + "','" + action + "')");
		}
		else {
			SqlDb.executeUpdate("UPDATE genre2user SET rating = rating + " + action + " WHERE genre_id = " + genreId + 
					"AND user_id = " + userId);
		}
		
		r = DbFuncs.getContent("SELECT COUNT(*) FROM artist2user WHERE artist_id='" + artistId + 
				"' AND user_id='" + userId + "'");
		
		try {
			numOfResults = r.getInt(1);
		}
		catch (SQLException e) {
			System.out.println("Exception: " + e.getMessage() );
		}
		if(numOfResults == 0) {
			SqlDb.executeQuery("INSERT INTO artist2user(artist_id, user_id, rating) VALUES('" + artistId + 
								"', " + userId + ",  " + action + ")");
		}
		else {
			SqlDb.executeUpdate("UPDATE artist2user SET rating = rating + " + action + " WHERE artist_id = " + artistId + 
								"AND user_id = " + userId);
		}
		try {
			r.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public static playlistEntry getRecomendedSong()
	{
		playlistEntry song = null;
		int userId = auth.Identity.getIdentity().getId();

		Statement stmt;
		ResultSet result, result1;
		Track t = null;
		
		try {
			stmt = SqlDb.getConnection().createStatement();
			result = stmt.executeQuery("SELECT genre_id FROM genre2user WHERE user_id = " + userId + " ORDER BY rating DESC");
			//result.next();
			
			int count = 0;
			
			int genreId1 = 0;
			int genreId2 = 0;			
			
			if(result.next() != false) {
				genreId1 = result.getInt(1);
				genreId2 = result.getInt(1);
			}
			else {
				Random gen = new Random();
				genreId1 = gen.nextInt(12);
				genreId2 = gen.nextInt(12);
			}
			if(result.next() != false) {
				genreId2 = result.getInt(1);
			}			
			
			ResultSet result_t = stmt.executeQuery("SELECT MAX(id) FROM tracks");
			result_t.next();
			int max_id = result_t.getInt(1);
			
			Random g = new Random();
			int start = g.nextInt(max_id - 200);
			int end = start + 200;
			
			result1 = stmt.executeQuery("SELECT tracks.id " +
										"FROM tracks, discs, genres, sub_genres " +
										"WHERE tracks.disc_id = discs.id AND" +
										"		discs.sub_genre_id = sub_genres.id AND" +
										"		(sub_genres.genre_id = " + genreId1 + " OR " +
												"sub_genres.genre_id = " + genreId2 + ")" +
												" AND tracks.id >= " + start + 
												" AND tracks.id <= " + end   + " AND rownum <= 30");
			int counter = 0;
			result1.next();
			
			while(counter < 1) {
				counter++;
				int i = result1.getInt(1);
				t = new Track(result1.getInt(1));
				
			}
		}
			catch (Exception e) {
				// TODO: handle exception
			}
			
			return new playlistEntry(t.getArtist(),t, t.getDisc(), t.getDisc().getSubGenre().getGenre());		

	}
}
